///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_CPATH_H_
#define _BUOLA_IMAGE_CPATH_H_

#include <buola/image/cpolygon.h>
#include <buola/mat/c2dtransform.h>
#include <buola/image/vertex/cbezier.h>

namespace buola { namespace img {

class CPath
{
public:
    enum EOperation
    {
        OP_MOVE,
        OP_LINE,
        OP_CUBIC,
        OP_QUAD,
        OP_DATA,
        OP_CLOSE        
    };

    enum EHints
    {
        HINT_NONE,
        HINT_CURVE=(1<<0),
        HINT_RECTILINEARSTROKE=(1<<1),
        HINT_RECTILINEARFILL=(1<<2),
        HINT_MAYBEREGION=(1<<3),
        HINT_EMPTY=HINT_RECTILINEARSTROKE|HINT_RECTILINEARFILL|HINT_MAYBEREGION
    };

    DECLARE_FRIEND_ENUM_BITOPS(EHints)

    struct SElement
    {
        SElement(EOperation pOp,const CPoint_d &pPoint)
            :   mOp(pOp)
            ,   mPoint(pPoint)
        {}

        EOperation mOp;
        CPoint_d mPoint;
    };
    
    CPath()
        :   mCurrentPoint(nUndefined)
        ,   mExtents(nUndefined)
        ,   mSubPathLength(0)
    {}

    template<typename tInterpreter>
    void Interpret(tInterpreter &pInterpreter) const;
    template<typename tInterpreter>
    void InterpretFlat(tInterpreter &pInterpreter) const;

private:
    void NewSubPath();
    SElement &LastMove()                {   return mElements[mElements.size()-mSubPathLength];    }
    const SElement &LastMove() const    {   return mElements[mElements.size()-mSubPathLength];    }
    void AddElement(EOperation pOp,const CPoint_d &pP);
    void EnsureMoveTo();

public:
    void Reset();
    
    void MoveTo(const CPoint_d &pP);
    void MoveTo(double pX,double pY)        {   MoveTo({pX,pY});        }
    void LineTo(const CPoint_d &pP);
    void LineTo(double pX,double pY)        {   LineTo({pX,pY});        }
    void RelMoveTo(const CPoint_d &pP);
    void RelMoveTo(double pX,double pY)     {   RelMoveTo({pX,pY});     }
    void RelLineTo(const CPoint_d &pP);
    void RelLineTo(double pX,double pY)     {   RelLineTo({pX,pY});     }
    void CurveTo(const CPoint_d &p1,const CPoint_d &p2,const CPoint_d &pE);
    void CurveTo(double pX1,double pY1,double pX2,double pY2,double pXE,double pYE)
    {
        CurveTo({pX1,pY1},{pX2,pY2},{pXE,pYE});
    }
        
    void CurveTo(const CPoint_d &p1,const CPoint_d &pE);

    void Arc(const CPoint_d &pCenter,double pR,double pA1=0,double pA2=2*M_PI);
    void RoundRect(const CRect_d &pR,double r);
    void Rect(const CRect_d &pR);
    void Rect(const CPoint_d &pP,const CSize_d &pS)     {   Rect(CRect_d(pP,pS));   }

    void Close();
    
    const std::vector<SElement> &Elements() const   {   return mElements;   }
    
private:
    void AddArc(const CPoint_d &pCenter,double pR,double pAngleStart,double pAngleSweep);
    
    CPoint_d mCurrentPoint;
    CRect_d mExtents;
    int mSubPathLength;

    std::vector<SElement> mElements;
};

template<typename tInterpreter>
void CPath::Interpret(tInterpreter &pInterpreter) const
{
    for(int i=0;i<mElements.size();i++)
    {
        switch(mElements[i].mOp)
        {
        case OP_MOVE:
            pInterpreter.MoveTo(mElements[i].mPoint);
            break;
        case OP_LINE:
            pInterpreter.LineTo(mElements[i].mPoint);
            break;
        case OP_CUBIC:
            pInterpreter.CurveTo(mElements[i].mPoint,mElements[i+1].mPoint,mElements[i+2].mPoint);
            i+=2;
            break;
        case OP_CLOSE:
            pInterpreter.Close(mElements[i].mPoint);
            break;
        default:
            break;
        }
    }

    pInterpreter.End();
}

template<typename tInterpreter>
void CPath::InterpretFlat(tInterpreter &pInterpreter) const
{
    for(int i=0;i<mElements.size();i++)
    {
        switch(mElements[i].mOp)
        {
        case OP_MOVE:
            pInterpreter.MoveTo(mElements[i].mPoint);
            break;
        case OP_LINE:
            pInterpreter.LineTo(mElements[i].mPoint);
            break;
        case OP_CUBIC:
            CBezier(mElements[i-1].mPoint,mElements[i].mPoint,mElements[i+1].mPoint,
                    mElements[i+2].mPoint).Interpret(pInterpreter);
            i+=2;
            break;
        case OP_CLOSE:
            pInterpreter.LineTo(mElements[i].mPoint);
            break;
        default:
            break;
        }
    }
}

/*namespace img*/ } /*namespace buola*/ }

#endif
